package com.jblv.frame.common.kafka.bean;

import kafka.serializer.Encoder;
import kafka.utils.VerifiableProperties;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;

/**
 * @author happyonion.yangtao
 * @version 1.0
 * @title ObjectEncoder
 * @projectName jblv-frame
 * @packageName com.jblv.web.kafka
 * @description
 * @date Create at 2020/4/17 14:00
 * @since jdk1.8
 */

public class ObjectEncoder implements Encoder<MqBaseMessage> {
    public ObjectEncoder() {
    }

    public ObjectEncoder(VerifiableProperties properties) {
    }

    @Override
    public byte[] toBytes(MqBaseMessage s) {
        byte[] b = null;
        ByteArrayOutputStream bo = null;
        ObjectOutputStream oo = null;

        try {
            bo = new ByteArrayOutputStream();
            oo = new ObjectOutputStream(bo);
            oo.writeObject(s);
            b = bo.toByteArray();
        } catch (IOException var18) {
            var18.printStackTrace();
        } finally {
            if (bo != null) {
                try {
                    bo.close();
                } catch (IOException var17) {
                    var17.printStackTrace();
                }

                bo = null;
            }

            if (oo != null) {
                try {
                    oo.close();
                } catch (IOException var16) {
                    var16.printStackTrace();
                }

                oo = null;
            }

        }

        return b;
    }
}
